package com.kevin.sharding.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kevin.sharding.model.User;
import com.kevin.sharding.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理")
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @Operation(summary = "分页列表", description = "分页列表")
    @GetMapping("/page")
    public IPage<User> page(@Parameter(name = "current", description = "页码", required = true)
                            @RequestParam(name = "current", defaultValue = "1") Long current,
                            @Parameter(name = "size", description = "数量", required = true)
                            @RequestParam(name = "size", defaultValue = "10") Long size) {
        return userService.page(new Page<User>().setCurrent(current).setSize(size));
    }

    @Operation(summary = "模拟添加用户", description = "模拟添加用户")
    @PostMapping
    public User add() {
        long currentTime = System.currentTimeMillis();
        User user = new User().setName("sharding_" + currentTime).setCreateTime(currentTime);
        boolean saved = userService.save(user);
        return saved ? user : null;
    }

}
